#include "pichoose_window.h"
#include "ui_pichoose_window.h"
#include "main_window.h"
#include <QPixmap>
extern Main_Window *pmain_window;
Pichoose_window::Pichoose_window(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Pichoose_window)
{
    ui->setupUi(this);
    ui->Back_menu->installEventFilter(this);
    ui->up->installEventFilter(this);
    ui->down->installEventFilter(this);
    ui->label_0->installEventFilter(this);
    ui->label_1->installEventFilter(this);
    ui->label_2->installEventFilter(this);
    ui->label_3->installEventFilter(this);
    ui->label_4->installEventFilter(this);
    ui->label_5->installEventFilter(this);
    ui->label_6->installEventFilter(this);
    ui->label_7->installEventFilter(this);

    Picshow_page = new Picshow_window();
    fileList = getFileNames(PicturePath);
    labelList = ui->widget->findChildren<QLabel*>();
    int i=0;
    picture_num = fileList.size();
    qDebug() << picture_num;
    Pichoose_show();
}

Pichoose_window::~Pichoose_window()
{
    delete ui;
}
void Pichoose_window::Pichoose_show(){
    QPixmap *pax = new QPixmap(":/picture/main_window/返回菜单.png");
    ui->Back_menu->setPixmap(*pax);
    ui->Back_menu->setScaledContents(true);

    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    ui->name_label->setPalette(pe);

    //显示图片
    int i=0;
    Picture_name="\0";
    while(i<8&&i<picture_num){
        Picture_name.append(PicturePath);
        Picture_name.append(fileList[i]);
      //  qDebug()<<Picture_name;
        labelList.at(i)->setPixmap(Picture_name);
        labelList.at(i)->setScaledContents(true);
        i++;
        Picture_name="\0";
    }
    QPixmap pict;
    pict.load(":/picture/picture_window/上一个.png");
    ui->up->setScaledContents(true);
    ui->up->setPixmap(pict);
    pict.load(":/picture/picture_window/下一个.png");
    ui->down->setPixmap(pict);
    ui->down->setScaledContents(true);
}

QStringList Pichoose_window::getFileNames(const QString &path)
{
  QDir dir(path);
  QStringList nameFilters;
  nameFilters << "*.jpg";
  QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
  return files;
}

bool Pichoose_window::eventFilter(QObject *obj, QEvent *event)
{
    QPixmap pic;
    if (obj == ui->Back_menu)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {
                    this->hide();
                 pmain_window->show();
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
    if (obj == ui->up)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {

                 now_local -=8;
                 if(now_local <0){
                     if(picture_num%8 == 0) now_local=picture_num-8;
                     else now_local=picture_num - (picture_num%8);
                 }
                 int i=now_local,j=0;
                 Picture_name="\0";
                 while(i<now_local+8){
                     if(i>=picture_num){
                         pic.load(":/f");

                         labelList.at(j)->setPixmap(pic);

                     }
                     else{
                        Picture_name.append(PicturePath);
                        Picture_name.append(fileList.at(i));
                   //     qDebug()<<Picture_name;
                        labelList.at(j)->setPixmap(Picture_name);
                        labelList.at(j)->setScaledContents(true);
                     }
                     i++;
                     j++;
                     Picture_name="\0";
                 }
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
    if (obj == ui->down)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {
                 now_local +=8;
                 if(now_local >picture_num) now_local=0;
                 int i=now_local,j=0;
                 Picture_name="\0";
                 while(i<now_local+8){
                     if(i>=picture_num){
                         pic.load(":/f");

                         labelList.at(j)->setPixmap(pic);

                     }
                     else{
                        Picture_name.append(PicturePath);
                        Picture_name.append(fileList.at(i));
                  //      qDebug()<<Picture_name;
                        labelList.at(j)->setPixmap(Picture_name);
                        labelList.at(j)->setScaledContents(true);
                     }
                     i++;
                     j++;
                     Picture_name="\0";
                 }
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
    if (obj == ui->label_0||obj == ui->label_1||obj == ui->label_2||obj == ui->label_3||obj == ui->label_4||obj == ui->label_5||obj == ui->label_6||obj == ui->label_7)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {
                 if(ui->label_0 == obj) Picshow_page->send_num(0);
                 else if(obj==ui->label_1) Picshow_page->send_num(1);
                 else if(obj==ui->label_2) Picshow_page->send_num(2);
                 else if(obj==ui->label_3) Picshow_page->send_num(3);
                 else if(obj==ui->label_4) Picshow_page->send_num(4);
                 else if(obj==ui->label_5) Picshow_page->send_num(5);
                 else if(obj==ui->label_6) Picshow_page->send_num(6);
                 else if(obj==ui->label_7) Picshow_page->send_num(7);
                 this->hide();
                 Picshow_page->show();
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
}
void Pichoose_window::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    QPen pen(QColor(255,255,255));
    QFont font;
    font.setFamily("Microsoft YaHei");
    painter.setPen(pen);

    /*背景颜色*/
    painter.setBrush(QColor(75,99,147));
    painter.drawRect(this->rect());
}
